package com.example.controller;


import com.example.model.EmailDetails;
import com.example.service.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/email")
public class EmailController {

    @Autowired
    private EmailService emailService;

    @GetMapping("/form")
    public String emailForm() {
        return "emailForm";
    }

    @PostMapping("/send")
    public String sendMailViaForm(@RequestParam String recipient,
                                  @RequestParam String subject,
                                  @RequestParam String messageBody,
                                  Model model) {
        EmailDetails emailDetails = new EmailDetails(recipient, subject, messageBody);
        String result = emailService.sendSimpleMail(emailDetails);
        model.addAttribute("message", result);
        return "emailForm";
    }
}
